home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch2 / Meta.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-06-16  |  7.6 KB  |  240 lines

  1. VERSION 5.00
  2. Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
  3. Begin VB.Form frmMeta 
  4.    AutoRedraw      =   -1  'True
  5.    Caption         =   "Meta"
  6.    ClientHeight    =   3405
  7.    ClientLeft      =   1950
  8.    ClientTop       =   1110
  9.    ClientWidth     =   4515
  10.    LinkTopic       =   "Form1"
  11.    PaletteMode     =   1  'UseZOrder
  12.    ScaleHeight     =   227
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   301
  15.    Begin MSComDlg.CommonDialog dlgMetafile 
  16.       Left            =   120
  17.       Top             =   120
  18.       _ExtentX        =   847
  19.       _ExtentY        =   847
  20.       _Version        =   393216
  21.       CancelError     =   -1  'True
  22.       Flags           =   2
  23.    End
  24.    Begin VB.Menu mnuFile 
  25.       Caption         =   "&File"
  26.       Begin VB.Menu mnuFileSaveAs 
  27.          Caption         =   "&Save As..."
  28.          Enabled         =   0   'False
  29.          Shortcut        =   ^A
  30.       End
  31.       Begin VB.Menu mnuFileOpen 
  32.          Caption         =   "&Open..."
  33.          Shortcut        =   ^O
  34.       End
  35.       Begin VB.Menu mnuFileClear 
  36.          Caption         =   "&Clear"
  37.          Shortcut        =   {DEL}
  38.       End
  39.       Begin VB.Menu mnuFileSep 
  40.          Caption         =   "-"
  41.       End
  42.       Begin VB.Menu mnuFileExit 
  43.          Caption         =   "E&xit"
  44.       End
  45.    End
  46. Attribute VB_Name = "frmMeta"
  47. Attribute VB_GlobalNameSpace = False
  48. Attribute VB_Creatable = False
  49. Attribute VB_PredeclaredId = True
  50. Attribute VB_Exposed = False
  51. Option Explicit
  52. Private Drawing As Boolean
  53. Private MetafileLoaded As Boolean
  54. Private PointX() As Single
  55. Private PointY() As Single
  56. Private NumPoints As Integer
  57. Private Declare Function CreateMetaFile Lib "gdi32" Alias "CreateMetaFileA" (ByVal lpString As String) As Long
  58. Private Declare Function CloseMetaFile Lib "gdi32" (ByVal hMF As Long) As Long
  59. Private Declare Function GetMetaFile Lib "gdi32" Alias "GetMetaFileA" (ByVal lpFileName As String) As Long
  60. Private Declare Function PlayMetaFile Lib "gdi32" (ByVal hdc As Long, ByVal hMF As Long) As Long
  61. Private Declare Function DeleteMetaFile Lib "gdi32" (ByVal hMF As Long) As Long
  62. Private Declare Function SetWindowExtEx Lib "gdi32" (ByVal hdc As Long, ByVal nX As Long, ByVal nY As Long, lpSize As SIZE) As Long
  63. Private Type SIZE
  64.     cx As Long
  65.     cy As Long
  66. End Type
  67. Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As Any) As Long
  68. Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
  69. ' Start in the current directory.
  70. Private Sub Form_Load()
  71.     dlgMetafile.InitDir = App.Path
  72. End Sub
  73. ' Start drawing.
  74. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  75.     Drawing = True
  76.     AddPoint -x, y
  77. End Sub
  78. ' Add a point to the list of points.
  79. Private Sub AddPoint(ByVal x As Single, ByVal y As Single)
  80.     ' Start over if a metafile is currently displayed.
  81.     If MetafileLoaded Then
  82.         Cls
  83.         MetafileLoaded = False
  84.         NumPoints = 0
  85.     End If
  86.     ' Add the new point.
  87.     NumPoints = NumPoints + 1
  88.     ReDim Preserve PointX(1 To NumPoints)
  89.     ReDim Preserve PointY(1 To NumPoints)
  90.     PointX(NumPoints) = x
  91.     PointY(NumPoints) = y
  92.     ' This represents the start of a new segment.
  93.     If x < 0 Then
  94.         CurrentX = -x
  95.         CurrentY = y
  96.     Else
  97.         Line -(x, y)
  98.     End If
  99.     mnuFileSaveAs.Enabled = True
  100. End Sub
  101. ' Continue drawing.
  102. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  103.     ' Do nothing if we are not drawing.
  104.     If Not Drawing Then Exit Sub
  105.     ' Add the point.
  106.     AddPoint x, y
  107. End Sub
  108. ' Stop drawing.
  109. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  110.     Drawing = False
  111. End Sub
  112. ' Clear the form.
  113. Private Sub mnuFileClear_Click()
  114.     Cls
  115.     NumPoints = 0
  116.     mnuFileSaveAs.Enabled = False
  117. End Sub
  118. ' Load a metafile.
  119. Private Sub mnuFileOpen_Click()
  120. Dim fname As String
  121. Dim hMF As Long
  122.     ' Allow the user to pick a file.
  123.     On Error Resume Next
  124.     dlgMetafile.FileName = "*.wmf"
  125.     dlgMetafile.Flags = cdlOFNFileMustExist + cdlOFNHideReadOnly
  126.     dlgMetafile.ShowOpen
  127.     If Err.Number = cdlCancel Then
  128.         ' The user clicked Cancel.
  129.         Unload dlgMetafile
  130.         Exit Sub
  131.     ElseIf Err.Number <> 0 Then
  132.         ' Unknown error.
  133.         Unload dlgMetafile
  134.         MsgBox "Error " & Format$(Err.Number) & _
  135.             " selecting file." & vbCrLf & _
  136.             Err.Description, vbExclamation
  137.         Exit Sub
  138.     End If
  139.     On Error GoTo LoadErr
  140.     ' Get the file name.
  141.     fname = dlgMetafile.FileName
  142.     dlgMetafile.InitDir = Left$(fname, Len(fname) _
  143.         - Len(dlgMetafile.FileTitle) - 1)
  144.     ' Load the metafile.
  145.     hMF = GetMetaFile(fname)
  146.     If hMF = 0 Then
  147.         MsgBox "Unable to load the metafile.", vbExclamation
  148.         Exit Sub
  149.     End If
  150.     ' Play the metafile.
  151.     Cls
  152.     If PlayMetaFile(hdc, hMF) = 0 Then
  153.         MsgBox "Error playing the metafile.", vbExclamation
  154.     End If
  155.     ' Delete the metafile to free resources.
  156.     If DeleteMetaFile(hMF) = 0 Then
  157.         MsgBox "Error deleting metafile " & _
  158.             fname & ".", vbExclamation
  159.     End If
  160.     Refresh
  161.     MetafileLoaded = True
  162.     mnuFileSaveAs.Enabled = False
  163.     Exit Sub
  164. LoadErr:
  165.     MsgBox "Error " & Format$(Err.Number) & _
  166.         " loading the metafile." & vbCrLf & _
  167.         Err.Description, vbExclamation
  168.     Exit Sub
  169. End Sub
  170. Private Sub mnuFileExit_Click()
  171.     Unload Me
  172. End Sub
  173. ' Save the drawing in a metafile.
  174. Private Sub mnuFileSaveAs_Click()
  175. Dim fname As String
  176. Dim i As Integer
  177. Dim mDC As Long
  178. Dim hMF As Long
  179. Dim x As Single
  180. Dim y As Single
  181. Dim old_size As SIZE
  182.     ' Allow the user to pick a file.
  183.     On Error Resume Next
  184.     dlgMetafile.FileName = "*.wmf"
  185.     dlgMetafile.Flags = cdlOFNOverwritePrompt + _
  186.         cdlOFNPathMustExist + cdlOFNHideReadOnly
  187.     dlgMetafile.ShowSave
  188.     If Err.Number = cdlCancel Then
  189.         ' The user canceled.
  190.         Unload dlgMetafile
  191.         Exit Sub
  192.     ElseIf Err.Number <> 0 Then
  193.         ' Unknown error.
  194.         Unload dlgMetafile
  195.         MsgBox "Error " & Format$(Err.Number) & _
  196.             " selecting file." & vbCrLf & _
  197.             Err.Description, vbExclamation
  198.         Exit Sub
  199.     End If
  200.     On Error GoTo SaveErr
  201.     ' Get the file name.
  202.     fname = dlgMetafile.FileName
  203.     dlgMetafile.InitDir = Left$(fname, Len(fname) _
  204.         - Len(dlgMetafile.FileTitle) - 1)
  205.     ' Create the metafile.
  206.     mDC = CreateMetaFile(ByVal fname)
  207.     If mDC = 0 Then
  208.         MsgBox "Error creating the metafile.", vbExclamation
  209.         Exit Sub
  210.     End If
  211.     ' Set the metafile's size to something reasonable.
  212.     SetWindowExtEx mDC, ScaleWidth, _
  213.         ScaleHeight, old_size
  214.     ' Draw in the metafile.
  215.     For i = 1 To NumPoints
  216.         x = PointX(i)
  217.         y = PointY(i)
  218.         If x < 0 Then
  219.             MoveToEx mDC, -x, y, vbNullString
  220.         Else
  221.             LineTo mDC, x, y
  222.         End If
  223.     Next i
  224.     ' Close the metafile.
  225.     hMF = CloseMetaFile(mDC)
  226.     If hMF = 0 Then
  227.         MsgBox "Error closing the metafile.", vbExclamation
  228.     End If
  229.     ' Delete the metafile to free resources.
  230.     If DeleteMetaFile(hMF) = 0 Then
  231.         MsgBox "Error deleting the metafile.", vbExclamation
  232.     End If
  233.     Exit Sub
  234. SaveErr:
  235.     MsgBox "Error " & Format$(Err.Number) & _
  236.         " saving file." & vbCrLf & _
  237.         Err.Description, vbExclamation
  238.     Exit Sub
  239. End Sub
  240.